//
//  YWObserveTableViewCell.m
//  maintain
//
//  Created by 烟雨云渊 on 2017/5/15.
//  Copyright © 2017年 ewin. All rights reserved.
//

#import "YWObserveTableViewCell.h"
#import "YWTroubleMissionModel.h"
#import "Masonry.h"
#import "YYLabel.h"
#import "YWMacro.h"
#import "YWCommonTool.h"
#import "UIColor+CustomColors.h"
#import "YWReportTroubleModel.h"
#import "YWHandler.h"
#import "YWContactManager.h"
#import "YWAttendanceRecord.h"
#import "YWKeepWatchMission.h"
#import "YWInspectionMissionModel.h"
#import "YWMaterialApply.h"
#import "YWMaterialStockOut.h"
#import "YWMaterialReturn.h"

@interface YWObserveTableViewCell()
@property (nonatomic, strong) YYLabel * observeLabel;
@end

@implementation YWObserveTableViewCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.layer.shouldRasterize = YES;
        self.layer.rasterizationScale = [UIScreen mainScreen].scale;
        [self setUpSubviews];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.separatorInset = UIEdgeInsetsMake(0, SCREEN_WIDTH, 0, 0);
    }
    return self;
}

- (void)setTroubleMissionModel:(YWTroubleMissionModel *)troubleMissionModel{
    _troubleMissionModel = troubleMissionModel;
    NSMutableArray *tempArray = [NSMutableArray array];
    NSMutableArray *observerArray = [NSMutableArray array];
    if (_troubleMissionModel.trouble.reporter) {
       [tempArray addObject:_troubleMissionModel.trouble.reporter];
    }
    if (_troubleMissionModel.creator) {
        [tempArray addObject:_troubleMissionModel.creator];
    }
    if (_troubleMissionModel.executor) {
        [tempArray addObject:_troubleMissionModel.executor];
    }
    
    for (YWHandler *handler in tempArray) {
        NSInteger uniqueId = handler.uniqueId;
        NSPredicate *searchPredicate = [NSPredicate predicateWithFormat:@"%K = %ld", @"uniqueId", uniqueId];
        NSArray *array = [[observerArray filteredArrayUsingPredicate:searchPredicate] mutableCopy];
        if (array.count==0) {
            [observerArray addObject:handler];
        }
    }
    NSString * firstStr = [NSString stringWithFormat:@"参与人(%ld): ",(long)observerArray.count];
    NSMutableString *observerName = [[NSMutableString alloc] init];
    [observerArray enumerateObjectsUsingBlock:^(YWHandler*  _Nonnull handler, NSUInteger idx, BOOL * _Nonnull stop) {
        if (observerName.length && idx!=0) {
            [observerName appendString:@" "];
        }
        [observerName appendString:handler.userName];
    }];
    
    NSString * nameStr = [NSString stringWithFormat:@"%@",observerName?:@""];//参与者要加进来
    
    _observeLabel.attributedText = [YWCommonTool attributedTextWithFirstText:firstStr firstColor:[UIColor customTextGrayBlackColor] secondText:nameStr secondColor:[UIColor customBlueColor] font:kContentFont];

    self.observeLabel.preferredMaxLayoutWidth = self.observeLabel.frame.size.width;
}

- (void)setAttendanceRecord:(YWAttendanceRecord *)attendanceRecord{
    _attendanceRecord = attendanceRecord;
    NSMutableArray *tempArray = [NSMutableArray array];
    NSMutableArray *observerArray = [NSMutableArray array];
    [tempArray addObject:_attendanceRecord.user];
    [tempArray addObjectsFromArray:_attendanceRecord.observers];
    for (YWHandler *handler in tempArray) {
        NSInteger uniqueId = handler.uniqueId;
        NSPredicate *searchPredicate = [NSPredicate predicateWithFormat:@"%K = %ld", @"uniqueId", uniqueId];
        NSArray *array = [[observerArray filteredArrayUsingPredicate:searchPredicate] mutableCopy];
        if (array.count==0) {
            [observerArray addObject:handler];
        }
    }
    NSString * firstStr = [NSString stringWithFormat:@"参与人(%ld): ",(long)observerArray.count];
    NSMutableString *observerName = [[NSMutableString alloc] init];
    [observerArray enumerateObjectsUsingBlock:^(YWHandler*  _Nonnull handler, NSUInteger idx, BOOL * _Nonnull stop) {
        if (observerName.length && idx!=0) {
            [observerName appendString:@" "];
        }
        [observerName appendString:handler.userName];
    }];
    
    NSString * nameStr = [NSString stringWithFormat:@"%@",observerName?:@""];//参与者要加进来
    
    _observeLabel.attributedText = [YWCommonTool attributedTextWithFirstText:firstStr firstColor:[UIColor customTextGrayBlackColor] secondText:nameStr secondColor:[UIColor customBlueColor] font:kContentFont];
    
    self.observeLabel.preferredMaxLayoutWidth = self.observeLabel.frame.size.width;
}

- (void)setMaintenanceMission:(YWMaintenanceMission *)maintenanceMission{
    //任务参与人，要把报障人算进去
    NSMutableArray *tempArray = [NSMutableArray array];
    NSMutableArray *observerArray = [NSMutableArray array];
    [tempArray addObject:maintenanceMission.creator];
    [tempArray addObjectsFromArray:maintenanceMission.executors];
    [tempArray addObjectsFromArray:maintenanceMission.observers];
    for (YWHandler *handler in tempArray) {
        NSInteger uniqueId = handler.uniqueId;
        NSPredicate *searchPredicate = [NSPredicate predicateWithFormat:@"%K = %ld", @"uniqueId", uniqueId];
        NSArray *array = [[observerArray filteredArrayUsingPredicate:searchPredicate] mutableCopy];
        if (array.count==0) {
            [observerArray addObject:handler];
        }
    }
    NSString * firstStr = [NSString stringWithFormat:@"参与人(%ld): ",(long)observerArray.count];
    NSMutableString *observerName = [[NSMutableString alloc] init];
    [observerArray enumerateObjectsUsingBlock:^(YWHandler*  _Nonnull handler, NSUInteger idx, BOOL * _Nonnull stop) {
        if (observerName.length && idx!=0) {
            [observerName appendString:@" "];
        }
        [observerName appendString:handler.userName];
    }];
    
    NSString * nameStr = [NSString stringWithFormat:@"%@",observerName?:@""];//参与者要加进来
    _observeLabel.attributedText = [YWCommonTool attributedTextWithFirstText:firstStr firstColor:[UIColor customTextGrayBlackColor] secondText:nameStr secondColor:[UIColor customBlueColor] font:kContentFont];
    
    self.observeLabel.preferredMaxLayoutWidth = self.observeLabel.frame.size.width;
}

- (void)setKeepWatchMission:(YWKeepWatchMission *)keepWatchMission{
    _keepWatchMission = keepWatchMission;
    NSMutableArray *tempArray = [NSMutableArray array];
    NSMutableArray *observerArray = [NSMutableArray array];
    [tempArray addObject:_keepWatchMission.creator];
    [tempArray addObjectsFromArray:_keepWatchMission.executors];
    [tempArray addObjectsFromArray:_keepWatchMission.observers];
    for (YWHandler *handler in tempArray) {
        NSInteger uniqueId = handler.uniqueId;
        NSPredicate *searchPredicate = [NSPredicate predicateWithFormat:@"%K = %ld", @"uniqueId", uniqueId];
        NSArray *array = [[observerArray filteredArrayUsingPredicate:searchPredicate] mutableCopy];
        if (array.count==0) {
            [observerArray addObject:handler];
        }
    }
    NSString * firstStr = [NSString stringWithFormat:@"参与人(%ld): ",(long)observerArray.count];
    NSMutableString *observerName = [[NSMutableString alloc] init];
    [observerArray enumerateObjectsUsingBlock:^(YWHandler*  _Nonnull handler, NSUInteger idx, BOOL * _Nonnull stop) {
        if (observerName.length && idx!=0) {
            [observerName appendString:@" "];
        }
        [observerName appendString:handler.userName];
    }];
    
    NSString * nameStr = [NSString stringWithFormat:@"%@",observerName?:@""];//参与者要加进来
    _observeLabel.attributedText = [YWCommonTool attributedTextWithFirstText:firstStr firstColor:[UIColor customTextGrayBlackColor] secondText:nameStr secondColor:[UIColor customBlueColor] font:kContentFont];
    
    self.observeLabel.preferredMaxLayoutWidth = self.observeLabel.frame.size.width;
}

- (void)setInspectionMission:(YWInspectionMissionModel *)inspectionMission{
    _inspectionMission = inspectionMission;
    NSMutableArray *tempArray = [NSMutableArray array];
    NSMutableArray *observerArray = [NSMutableArray array];
    [tempArray addObject:_inspectionMission.creator];
    [tempArray addObjectsFromArray:_inspectionMission.executors];
    [tempArray addObjectsFromArray:_inspectionMission.observers];
    for (YWHandler *handler in tempArray) {
        NSInteger uniqueId = handler.uniqueId;
        NSPredicate *searchPredicate = [NSPredicate predicateWithFormat:@"%K = %ld", @"uniqueId", uniqueId];
        NSArray *array = [[observerArray filteredArrayUsingPredicate:searchPredicate] mutableCopy];
        if (array.count==0) {
            [observerArray addObject:handler];
        }
    }
    NSString * firstStr = [NSString stringWithFormat:@"参与人(%ld): ",(long)observerArray.count];
    NSMutableString *observerName = [[NSMutableString alloc] init];
    [observerArray enumerateObjectsUsingBlock:^(YWHandler*  _Nonnull handler, NSUInteger idx, BOOL * _Nonnull stop) {
        if (observerName.length && idx!=0) {
            [observerName appendString:@" "];
        }
        [observerName appendString:handler.userName];
    }];
    
    NSString * nameStr = [NSString stringWithFormat:@"%@",observerName?:@""];//参与者要加进来
    _observeLabel.attributedText = [YWCommonTool attributedTextWithFirstText:firstStr firstColor:[UIColor customTextGrayBlackColor] secondText:nameStr secondColor:[UIColor customBlueColor] font:kContentFont];
    
    self.observeLabel.preferredMaxLayoutWidth = self.observeLabel.frame.size.width;
}

- (void)setApply:(YWMaterialApplyViewModel *)apply{
    _apply = apply;
    //申请人、接收人、参与人
    NSMutableArray *tempArray = [NSMutableArray array];
    NSMutableArray *observerArray = [NSMutableArray array];
    [tempArray addObject:_apply.creator];
    if (_apply.rejectUser) {
        [tempArray addObject:_apply.rejectUser];
    }
    [tempArray addObjectsFromArray:_apply.receivers];
    [tempArray addObjectsFromArray:_apply.observers];
    for (YWHandler *handler in tempArray) {
        NSInteger uniqueId = handler.uniqueId;
        NSPredicate *searchPredicate = [NSPredicate predicateWithFormat:@"%K = %ld", @"uniqueId", uniqueId];
        NSArray *array = [[observerArray filteredArrayUsingPredicate:searchPredicate] mutableCopy];
        if (array.count==0) {
            [observerArray addObject:handler];
        }
    }
    NSString * firstStr = [NSString stringWithFormat:@"参与人(%ld): ",(long)observerArray.count];
    NSMutableString *observerName = [[NSMutableString alloc] init];
    [observerArray enumerateObjectsUsingBlock:^(YWHandler*  _Nonnull handler, NSUInteger idx, BOOL * _Nonnull stop) {
        if (observerName.length && idx!=0) {
            [observerName appendString:@" "];
        }
        [observerName appendString:handler.userName];
    }];
    
    NSString * nameStr = [NSString stringWithFormat:@"%@",observerName?:@""];//参与者要加进来
    _observeLabel.attributedText = [YWCommonTool attributedTextWithFirstText:firstStr firstColor:[UIColor customTextGrayBlackColor] secondText:nameStr secondColor:[UIColor customBlueColor] font:kContentFont];
    
    self.observeLabel.preferredMaxLayoutWidth = self.observeLabel.frame.size.width;
}

- (void)setStockOut:(YWMaterialStockOut *)stockOut{
    _stockOut = stockOut;
    //申请人、接收人、参与人
    NSMutableArray *tempArray = [NSMutableArray array];
    NSMutableArray *observerArray = [NSMutableArray array];
    [tempArray addObject:_stockOut.creator];
    [tempArray addObject:_stockOut.receiver];
    [tempArray addObjectsFromArray:_stockOut.observers];
    for (YWHandler *handler in tempArray) {
        NSInteger uniqueId = handler.uniqueId;
        NSPredicate *searchPredicate = [NSPredicate predicateWithFormat:@"%K = %ld", @"uniqueId", uniqueId];
        NSArray *array = [[observerArray filteredArrayUsingPredicate:searchPredicate] mutableCopy];
        if (array.count==0) {
            [observerArray addObject:handler];
        }
    }
    NSString * firstStr = [NSString stringWithFormat:@"参与人(%ld): ",(long)observerArray.count];
    NSMutableString *observerName = [[NSMutableString alloc] init];
    [observerArray enumerateObjectsUsingBlock:^(YWHandler*  _Nonnull handler, NSUInteger idx, BOOL * _Nonnull stop) {
        if (observerName.length && idx!=0) {
            [observerName appendString:@" "];
        }
        [observerName appendString:handler.userName];
    }];
    
    NSString * nameStr = [NSString stringWithFormat:@"%@",observerName?:@""];//参与者要加进来
    _observeLabel.attributedText = [YWCommonTool attributedTextWithFirstText:firstStr firstColor:[UIColor customTextGrayBlackColor] secondText:nameStr secondColor:[UIColor customBlueColor] font:kContentFont];
    
    self.observeLabel.preferredMaxLayoutWidth = self.observeLabel.frame.size.width;
}

- (void)setReturnBack:(YWMaterialReturn *)returnBack{
    _returnBack = returnBack;
    //申请人、接收人、参与人
    NSMutableArray *tempArray = [NSMutableArray array];
    NSMutableArray *observerArray = [NSMutableArray array];
    [tempArray addObject:_returnBack.creator];
    [tempArray addObject:_returnBack.returner];
    [tempArray addObjectsFromArray:_returnBack.observers];
    for (YWHandler *handler in tempArray) {
        NSInteger uniqueId = handler.uniqueId;
        NSPredicate *searchPredicate = [NSPredicate predicateWithFormat:@"%K = %ld", @"uniqueId", uniqueId];
        NSArray *array = [[observerArray filteredArrayUsingPredicate:searchPredicate] mutableCopy];
        if (array.count==0) {
            [observerArray addObject:handler];
        }
    }
    NSString * firstStr = [NSString stringWithFormat:@"参与人(%ld): ",(long)observerArray.count];
    NSMutableString *observerName = [[NSMutableString alloc] init];
    [observerArray enumerateObjectsUsingBlock:^(YWHandler*  _Nonnull handler, NSUInteger idx, BOOL * _Nonnull stop) {
        if (observerName.length && idx!=0) {
            [observerName appendString:@" "];
        }
        [observerName appendString:handler.userName];
    }];
    
    NSString * nameStr = [NSString stringWithFormat:@"%@",observerName?:@""];//参与者要加进来
    _observeLabel.attributedText = [YWCommonTool attributedTextWithFirstText:firstStr firstColor:[UIColor customTextGrayBlackColor] secondText:nameStr secondColor:[UIColor customBlueColor] font:kContentFont];
    
    self.observeLabel.preferredMaxLayoutWidth = self.observeLabel.frame.size.width;
}

- (void)setUpSubviews{
    [self.observeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.contentView).offset(6);
        make.left.mas_equalTo(self.contentView).offset(8);
        make.right.mas_equalTo(self.contentView).offset(-8);
        make.bottom.mas_equalTo(self.contentView.mas_bottom);
    }];
}

- (YYLabel *)observeLabel{
    if (!_observeLabel) {
        YYLabel *label = [[YYLabel alloc] init];
        label.font = kContentFont;
        label.textColor = [UIColor blackColor];
        label.numberOfLines = 0;
        _observeLabel = label;
        [self.contentView addSubview:_observeLabel];
    }
    return _observeLabel;
}


@end
